/**
 * 
 * 
 * 
 * Form Add family 
 * 
 * Project for Fundació Concepció Juvanteny
 * 
 * 
 * 
 * @author Felipe
 * 
 * 
 * 
 * 
 */




/**
 * Family name
 * @returns String
 */

FCJ_Family_DATA.prototype.name = null;
FCJ_Family_DATA.prototype.phone = null;
FCJ_Family_DATA.prototype.educator = null;
FCJ_Family_DATA.prototype.mail = null;
FCJ_Family_DATA.prototype.payment_Method = null;
FCJ_Family_DATA.prototype.transport_Method = null;
FCJ_Family_DATA.prototype.transport_Method__Other_Detail = null;

/**
 * Family data
 */
function FCJ_Family_DATA() {
	
}


FCJ_FamilyMember_DATA.prototype._deleted = false;
FCJ_FamilyMember_DATA.prototype._layer_ID = null;
FCJ_FamilyMember_DATA.prototype.name = null;
FCJ_FamilyMember_DATA.prototype.surname = null;
FCJ_FamilyMember_DATA.prototype.dni = null;
FCJ_FamilyMember_DATA.prototype.dateofbirth = null;
FCJ_FamilyMember_DATA.prototype.specialAttentions = null;
FCJ_FamilyMember_DATA.prototype.member_isReferent = null;
FCJ_FamilyMember_DATA.prototype.member_Ref_Phone = null;


/**
 * Family member data
 */
function FCJ_FamilyMember_DATA() {
	
}




FCJ_Ref_User_DATA.prototype.user_ID = null;
FCJ_Ref_User_DATA.prototype.userName = null;

/**
 * Family member data
 */
function FCJ_Ref_User_DATA() {
	
}




/**
 * Form family event
 * @returns {FCJ_form_Add_Family_Event}
 */
function FCJ_form_Add_Family_Event() {

}


/**
 * Form add family CONSTANTS
 */
var FCJ_form_Add_Famimly__CONSTANTS = {
	
	"FORM_MODE_LIST" : "MODE_LIST",
	"FORM_MODE_CREATE" : "MODE_CREATE",
	"FORM_MODE_EDIT" : "MODE_EDIT",
	
	"LAYER_ID__Member_List_Items" : "members_List_Items",
	"LAYER_ID__Member_List_Item" : "member_List_Item",
	"LAYER_ID__Member_List_Options__Num_Members__Value" : "members_List_Options__Num_Members__Value",
	"LAYER_ID__Member_List_Item_Description" : "member_List_Item_Description",
	"LAYER_ID__Member_List_Item_Options" : "member_List_Item_Options",
	"LAYER_ID__Member_List_Item_Option_Edit" : "member_List_Item_Option_Edit",
	"LAYER_ID__Member_List_Item_Option_Delete" : "member_List_Item_Option_Delete",
	"LAYER_ID__Member_Data" : "layer__Member_DATA",
	"LAYER_ID__Member_Ref_Phone" : "layer__Member_Ref_Phone",
	"LAYER_ID__Transport_Type__OTHER_Detail" : "layer__Transport_Type__OTHER_Detail",

	"LAYER_ID__Family_Form" : "family_Form",
	"LAYER_ID__Member_Form" : "member_Form",
	"LAYER_ID__ModalWindow__DNI_Duplicated" : "modalWindow__DNI_Duplicated",
	"LAYER_ID__ModalWindow__NewMember_Added" : "modalWindow__NewMember_Added",
	"LAYER_ID__ModalWindow__Member_Updated" : "modalWindow__Member_Updated",
	"LAYER_ID__ModalWindow__Family_DATA__Problem" : "modalWindow__Family_DATA__Problem",
	"LAYER_ID__ModalWindow__Member_DATA__Problem" : "modalWindow__Member_DATA__Problem",
	"LAYER_ID__ModalWindow__No_Members_Found" : "modalWindow__No_Members_Found",
	"LAYER_ID__ModalWindow__Data_Sent_OK" : "modalWindow__Data_Sent_OK",
	"LAYER_ID__ModalWindow__Data_Submit_Error" : "modalWindow__Data_Submit_Error",
	"LAYER_ID__ModalWindow__Data_Submit_Error__Detail" : "modalWindow__Data_Submit_Error__Detail",
	
	"LAYER_ID__Button_New_Member" : "button_New_Member",
	"LAYER_ID__Button_Cancel_New_Member" : "button_Cancel_NewMember",
	"LAYER_ID__Button_Send_Data" : "button_SendData",
	"LAYER_ID__Button_Member_isReferent" : "member_isReferent",
	"LAYER_ID__Button_Tranport_Type" : "transport_Type",
	"LAYER_ID__Button_UpdateMember" : "button_UpdateMember",
	"LAYER_ID__Button_ReloadForm" : "button_reloadForm",
	
	"EVENT__Add_Member" : "Add_Member",
	"EVENT__Update_Member" : "Update_Member",
	"EVENT__Edit_Member" : "Edit_Member",
	"EVENT__Delete_Member" : "Delete_Member",
	"EVENT__Send_Data" : "Send_Data",
	"EVENT__New_Member" : "New_Member",
	"EVENT__Cancel_New_Member" : "Cancel_New_Member"

};


/**
 * Return the number of family members
 */
FCJ_form_Add_Famimly.prototype.get_FamilyMembers_Number = function() {
	return this.config.familyMembers_DATA.length;
};


/**
 * Return the number of active (not deleted) family members
 */
FCJ_form_Add_Famimly.prototype.get_FamilyMembers_Active_Number = function() {
	
	var counter = 0;
	
	for ( var int = 0; int < this.config.familyMembers_DATA.length; int++) {
		
		/** @type FCJ_FamilyMember_DATA */
		var family_Member = this.config.familyMembers_DATA[int];
		if (!family_Member._deleted) {
			counter++;
		}
		
	}
	
	return counter;
};



/**
 * Return a family position number selected by dni
 * 
 * @param dni DNI of the family member
 * 
 * @returns Number {-1 if not found}
 */
FCJ_form_Add_Famimly.prototype.get_FamilyMember_Position_By_DNI = function(dni) {
	
//	var found = false;
	var position = -1;
	
	for ( var int = 0; int < this.config.familyMembers_DATA.length; int++) {
		
		/** @type FCJ_FamilyMember_DATA */
		var family_Member = this.config.familyMembers_DATA[int];
		if (family_Member.dni == dni && !family_Member._deleted) {
//			found = true;
			position = int;
			break;
		}
		
	}
	
	return position;

};


/**
 * Return a family member selected by dni
 * 
 * @param dni DNI of the family member
 * 
 * @returns {FCJ_FamilyMember_DATA} or {false}
 */
FCJ_form_Add_Famimly.prototype.get_FamilyMember_By_DNI = function(dni) {
	
	var found = false;
	var result = false;
	var position = -1;
	
	position = this.get_FamilyMember_Position_By_DNI(dni);
	
	if (position > -1) {
		result = this.config.familyMembers_DATA[position];
	} 
	
	return result;
};


/**
 * Return a family member selected by dni
 * 
 * @param ref_User_ID DNI of the family member
 * 
 * @returns {FCJ_FamilyMember_DATA} or {false}
 */
FCJ_form_Add_Famimly.prototype.get_Ref_User_By_ID = function(ref_User_ID) {
	
	var found = false;
	var result = false;
	var position = -1;
	
	
	for ( var int = 0; int < this.config.referent_Users.ref_Users_DATA.length; int++) {
		
		/** @type FCJ_FamilyMember_DATA */
		var ref_User = this.config.familyMembers_DATA[int];
		if (ref_User.id == ref_User_ID) {
			found = true;
			position = int;
			break;
		}
		
	}
	
	
	if (found) {
		result = this.config.familyMembers_DATA[position];
	} 
	
	return result;
};



/**
 * Method for add family member
 * @param {FCJ_FamilyMember_DATA} familyMember_DATA
 * 
 */
FCJ_form_Add_Famimly.prototype.add_FamilyMember = function(familyMember_DATA) {
	
	this.config.familyMembers_DATA.push(familyMember_DATA);
	
};


/**
 * Method for get family data from form
 * @return {FCJ_Family_DATA}
 * 
 */
FCJ_form_Add_Famimly.prototype.getForm__FamilyDATA = function() {
	
	var family_DATA = new FCJ_Family_DATA();
	family_DATA.name = jQuery('#family').val();
	family_DATA.phone = jQuery('#phone').val();
	family_DATA.educator = this.config.family_DATA.educator; //jQuery('#educator').val();	// Educator is set by autocomplete
	family_DATA.mail = jQuery('#mail').val();
	family_DATA.payment_Method = jQuery('input[name=payment_Type]:checked', '#family_Form').val();
	family_DATA.transport_Method = jQuery('input[name=transport_Type]:checked', '#family_Form').val();
	family_DATA.transport_Method__Other_Detail = jQuery('#transport_Type__OTHER_Detail').val();
	
	return family_DATA;
	
};


/**
 * Method for get family member data from form
 * @return {FCJ_FamilyMember_DATA}
 * 
 */
FCJ_form_Add_Famimly.prototype.getForm__FamilyMemberDATA = function() {
	
	var family_Member_DATA = new FCJ_FamilyMember_DATA();
	
	family_Member_DATA.name = jQuery('#member_Name').val();
	family_Member_DATA.surname = jQuery('#member_SurName').val();
	family_Member_DATA.dni = jQuery('#member_DNI').val();
	family_Member_DATA.dateofbirth = jQuery('#member_Date_of_birth').val();
	
//	family_Member_DATA.member_isReferent = jQuery('#member_isReferent').val();
	if (jQuery('#member_isReferent').is(":checked")) {
		family_Member_DATA.member_isReferent = true;
	} else {
		family_Member_DATA.member_isReferent = false;
	}
	
	family_Member_DATA.member_Ref_Phone = jQuery('#member_Ref_Phone').val();
	family_Member_DATA.specialAttentions = jQuery('#member_specialAttentions').val();

	
	return family_Member_DATA;
	
};


/**
 * Method for set family member data from form
 * @param {FCJ_FamilyMember_DATA} family_Member_DATA
 *
 * 
 */
FCJ_form_Add_Famimly.prototype.setForm__FamilyMemberDATA = function(family_Member_DATA) {
	
	jQuery('#member_Name').val(family_Member_DATA.name);
	jQuery('#member_SurName').val(family_Member_DATA.surname);
	jQuery('#member_DNI').val(family_Member_DATA.dni);
	
		
	// TODO FCJ Remove Debug ----------------------|\/|---	
	if (console != null) {
		console.log("Paint Family member data");
		console.log(family_Member_DATA);
	}
	// --------------------------------------------|/\|---
	
	
//	jQuery('#member_isReferent').val(family_Member_DATA.member_isReferent);
	
	if (family_Member_DATA.member_isReferent) {
		jQuery('#member_isReferent').prop('checked', true);
		jQuery('#member_isReferent').change();
	} else {
		jQuery('#member_isReferent').prop('checked', false);
		jQuery('#member_isReferent').change();
	}
	
	
	jQuery('#member_Ref_Phone').val(family_Member_DATA.member_Ref_Phone);
	
	
	jQuery('#member_specialAttentions').val(family_Member_DATA.specialAttentions);
	
};


/**
 * Method for set family member data from form
 * @param {FCJ_FamilyMember_DATA} family_Member_DATA
 *
 * 
 */
FCJ_form_Add_Famimly.prototype.paint_MemberListItem = function(family_Member_DATA) {
	
	var position_ID = family_Member_DATA._layer_ID;
	
//	var layer_Member = jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item + '__' + position_ID);
	
	// Set HTML template and change ID's to new family member ---------------------------------------------------------------------------|\/|---
//	jQuery(layer_Member)
//		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item + '__' + position_ID);
	
//	jQuery(layer_Member).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Description)
//		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Description + '__' + position_ID);
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Description + '__' + position_ID)
		.html(family_Member_DATA.name + ' ' + family_Member_DATA.surname + ' ' + family_Member_DATA.dni);
//	
//	jQuery(layer_Member).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Options)
//		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Options + '__' + position_ID);
//	jQuery(layer_Member).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Edit)
//		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Edit + '__' + position_ID);
//	jQuery(layer_Member).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Delete)
//		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Delete + '__' + position_ID);
	// ----------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	// TODO FCJ Remove Debug ----------------------|\/|---	
	if (console != null) {
		console.log("Paint Family member list item");
		console.log(family_Member_DATA);
	}
	// --------------------------------------------|/\|---
	
	
};


/**
 * Method for read related users into form
 * 
 */
FCJ_form_Add_Famimly.prototype.load_RelatedUsers = function() {
	
	var form_Add_Family = this; // Reference to this object
	
	// Send data to server ---------------------------------------------------------------------------------------------------------------------------|\ Send data to server /|---
	jQuery.ajax({
        type: 'POST',
        url: 'lib/form_ADD_Family.php?function=loadRefUsers',
        data: { "no_data" : null },
        success: function(data) {
    		
    		var data_Object = jQuery.parseJSON(data); // $.parseJSON(ajaxResponse);
    		form_Add_Family.config.referent_Users.ref_Users_DATA = data_Object.referent_Users;
    		
    		
    		// Prepare autocomplete source ----------------------------------------------------------------------------------------------|\/|---
    		var autoCompleteSource = [];
    		
    		for ( var index_Ref_User = 0; index_Ref_User < form_Add_Family.config.referent_Users.ref_Users_DATA.length; index_Ref_User++) {
				
    			var ref_User = form_Add_Family.config.referent_Users.ref_Users_DATA[index_Ref_User];
				
				var ref_User_Source = new Object();
				ref_User_Source.label = ref_User.user_Name;
				ref_User_Source.value = ref_User.id;
				
				autoCompleteSource.push(ref_User_Source);
			}
    		// --------------------------------------------------------------------------------------------------------------------------|/\|---

    		
    		// Initialize autocomplete plug-in -------------------------------------------------------|\/|---
    		jQuery( "#educator").autocomplete({ 
    			source: autoCompleteSource,
//    			source: [ 
//    			          { label: "Choice1", value: "value1" },
//    			          { label: "Choice1", value: "value1" }
//    			          ],
//    			source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]
    			minLength: 0,
    			change: function( event, ui ) {
    	            if ( !ui.item ) {
    	            	// If no item selected clear field and data ---------------|\/|---
    	            	form_Add_Family.config.family_DATA.educator = null;
    	            	jQuery("#educator").val("");
	            		// --------------------------------------------------------|/\|---
    	            }
    	      }
    		});
    		// ---------------------------------------------------------------------------------------|/\|---
    		
    		
    		// Search on focus event -------------------------------------------------------------------------------|\/|---
    		jQuery('#educator').bind('focus', function() {
    			jQuery('#educator').autocomplete( "search", "" );
    		});
    		// -----------------------------------------------------------------------------------------------------|/\|---	
    		
    		
    		// Autocomplete item selection event -------------------------------------------------------------------|\/|---
    		jQuery('#educator').bind('autocompleteselect', function( event, ui ) {
            
    			event.preventDefault();
    			
    			jQuery("#educator").val(ui.item.label);
    			form_Add_Family.config.family_DATA.educator = ui.item.value;
    			
    			
    			// TODO FCJ Remove Debug ----------------------|\/|---	
            	if (console != null) {
            		console.log("Autocomplete item selected");
            		console.log(event);
            		console.log(ui);
//            		console.log(autoCompleteSource);
            	}
            	// --------------------------------------------|/\|---
    		
    		});
    		// -----------------------------------------------------------------------------------------------------|/\|---	

    		
        	// TODO FCJ Remove Debug ----------------------|\/|---	
        	if (console != null) {
        		console.log("Load related users OK");
        		console.log(data);
        		console.log(form_Add_Family.config.referent_Users);
        		console.log(autoCompleteSource);
        	}
        	// --------------------------------------------|/\|---
		
        }
	});
	// -----------------------------------------------------------------------------------------------------------------------------------------------|/ Send data to server \|---
	
};


/**
 * Set form mode
 */
FCJ_form_Add_Famimly.prototype.set_Form_MODE = function(mode) {
	
	switch (mode) {
	
		// List Mode -----------------------------------------------------------------------------------------------------------------------------|\ List Mode /|---
		case FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST:
			
			this.config.form_Mode = mode;
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Data).hide();
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_New_Member).removeAttr('disabled');
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Send_Data).removeAttr('disabled');
			break;
		// ---------------------------------------------------------------------------------------------------------------------------------------|/ List Mode \|---			
			
		// Create Mode -------------------------------------------------------------------------------------------------------------------------|\ Create Mode /|---
		case FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_CREATE:
			
			this.config.form_Mode = mode;
			
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_New_Member).attr('disabled', 'disabled');
			
			jQuery('#member_DNI').removeAttr('disabled');

//			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_New_Member).show();
//			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_UpdateMember).hide();

			jQuery('#button_NewMember').show();
			jQuery('#button_UpdateMember').hide();
			
			
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Send_Data).attr('disabled', 'disabled');
			
			this.config.others.member_DATA__Validator.resetForm();
			
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Data).attr('class', 'member_DATA');
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Data).show();
			
			break;
		// -------------------------------------------------------------------------------------------------------------------------------------|/ Create Mode \|---			
			
		case FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_EDIT:
			
			this.config.form_Mode = mode;
			
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_New_Member).attr('disabled', 'disabled');
			
			jQuery('#member_DNI').attr('disabled', 'disabled');
			
//			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_New_Member).hide();
//			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_UpdateMember).show();
			
			jQuery('#button_NewMember').hide();
			jQuery('#button_UpdateMember').show();
			
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Send_Data).attr('disabled', 'disabled');
			
			this.config.others.member_DATA__Validator.resetForm();
			
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Data).attr('class', 'member_DATA__Edit_Mode');
			jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Data).show();

			break;			

			
		default:
			
			return this.set_Form_MODE(FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST);
		
			break;
	}
	
};



/**
 * Reload form
 */
FCJ_form_Add_Famimly.prototype.reload_Form = function() {
	
	location.reload(true); // reload current location without browser cache
};
	


/**
 * Manage event { add family member }
 */
FCJ_form_Add_Famimly.prototype.manage_Event__add_FamilyMember = function() {
	
	// Initialize required objects --------------------------------------------------------------|\/|---
	var family_Member_DATA = null;
	var position_ID = this.get_FamilyMembers_Number();
	var html_DATA = jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item).clone();
	var form_Add_Family = this;
	// ------------------------------------------------------------------------------------------|/\|---	
		
	
	// Validate form -------------------------------------------------------------------------------------------------------------------------------------|\/|---
	if (!jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Form).validate().form()) {
		

		// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
		jQuery(function() {
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_DATA__Problem ).dialog({
				  width: 560,
			      height: 240,
			      modal: true,
			      buttons: {
		                Ok: function () {
		                	jQuery(this).dialog("close");
		                }
		            }
			    });
			  });
		
		setTimeout(function(){ 
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_DATA__Problem ).dialog("close");
			},
			2470);
		// -------------------------------------------------------------------------------------------------------------------|/\|---
		
		
		return;
	}
	// ---------------------------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Set family member data --------------------------------------------------------------|\/|---
	family_Member_DATA = this.getForm__FamilyMemberDATA();
	family_Member_DATA._layer_ID = position_ID;
	// -------------------------------------------------------------------------------------|/\|---	
	
	
	// Check duplicated member by DNI -------------------------------------------------------------------------------------|\/|---
	if ( this.get_FamilyMember_By_DNI(family_Member_DATA.dni) ) {
		
		this.modalWindow(jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__DNI_Duplicated).html());
		
		return;
	}
	// --------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Set HTML template and change ID's to new family member ---------------------------------------------------------------------------|\/|---
	jQuery(html_DATA)
		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item + '__' + position_ID);
	
	jQuery(html_DATA).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Description)
		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Description + '__' + position_ID);
	jQuery(html_DATA).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Description + '__' + position_ID)
		.html(family_Member_DATA.name + ' ' + family_Member_DATA.surname + ' ' + family_Member_DATA.dni);
	
	jQuery(html_DATA).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Options)
		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Options + '__' + position_ID);
	jQuery(html_DATA).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Edit)
		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Edit + '__' + position_ID);
	jQuery(html_DATA).find('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Delete)
		.attr('id', FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Delete + '__' + position_ID);
	// ----------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Add new HTML code into Member list items layer --------------------------------------------------|-|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Items).append(html_DATA);
	
	
	// Add family member into configuration --------------------|-|---
	this.add_FamilyMember(family_Member_DATA);
	
	
	// Update familiy members number counter --------------------------------------------------------------------------------------------------------|-|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Options__Num_Members__Value).html(this.get_FamilyMembers_Active_Number()); 
	
	
	// Event action mapping {Edit} ------------------------------------------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Edit + '__' + position_ID).bind('click', function() {
		
		var event = new FCJ_form_Add_Family_Event();
		event.eventType = FCJ_form_Add_Famimly__CONSTANTS.EVENT__Edit_Member;
		event.eventMessage = family_Member_DATA;
		
		form_Add_Family.event_Driver.notifyEvent(event, null, form_Add_Family);
		
	});
	// ----------------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Event action mapping {Delete} ----------------------------------------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item_Option_Delete + '__' + position_ID).bind('click', function() {
		
		var event = new FCJ_form_Add_Family_Event();
		event.eventType = FCJ_form_Add_Famimly__CONSTANTS.EVENT__Delete_Member;
		event.eventMessage = family_Member_DATA;
		
		form_Add_Family.event_Driver.notifyEvent(event, null, form_Add_Family);
		
	});
	// ----------------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Return to list mode -------------------------------------------------------------|-|---
	this.set_Form_MODE(FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST);
	
	
	// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
	jQuery(function() {
		jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__NewMember_Added ).dialog({
		      height: 140,
		      modal: true
		    });
		  });
	
	setTimeout(function(){ 
		jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__NewMember_Added ).dialog("close");
		},
		1630);
	// -------------------------------------------------------------------------------------------------------------------|/\|---
	
};


/**
 * Manage event { update family member }
 */
FCJ_form_Add_Famimly.prototype.manage_Event__update_FamilyMember = function() {
	
	// Validate form ---------------------------------------------------------------------------------------------------------------------------------------|\/|---
	if (!jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Form).validate().form()) {
		
		// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
		jQuery(function() {
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_DATA__Problem ).dialog({
				  width: 560,
			      height: 240,
			      modal: true,
			      buttons: {
		                Ok: function () {
		                	jQuery(this).dialog("close");
		                }
		            }
			    });
			  });
		
		setTimeout(function(){ 
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_DATA__Problem ).dialog("close");
			},
			2470);
		// -------------------------------------------------------------------------------------------------------------------|/\|---
		
		
		return;
	}
	// -----------------------------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	var member_DATA__edited = this.getForm__FamilyMemberDATA();
	
	var position = this.get_FamilyMember_Position_By_DNI(member_DATA__edited.dni);
	
	if (position < 0) {
		return;
	}

	
	// TODO FCJ Remove Debug ----------------------|\/|---	
	if (console != null) {
		console.log("Update family member");
//		console.log(event);
//		console.log(ui);
//		console.log(autoCompleteSource);
	}
	// --------------------------------------------|/\|---
	member_DATA__edited._layer_ID = position;
	this.config.familyMembers_DATA[position] = member_DATA__edited;
	this.paint_MemberListItem(member_DATA__edited);
	
	this.set_Form_MODE(FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST);
	

	// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
	jQuery(function() {
		jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_Updated ).dialog({
		      height: 140,
		      modal: true
		    });
		  });
	
	setTimeout(function(){ 
		jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_Updated ).dialog("close");
		},
		1630);
	// -------------------------------------------------------------------------------------------------------------------|/\|---
	
	
};
		

/**
 * Send data
 * @param {FCJ_form_Add_Family_Event} event
 */
FCJ_form_Add_Famimly.prototype.manage_Event__send_DATA = function(event) {
	

	// Validate form -------------------------------------------------------------------------------------------------------------------------------------|\/|---
	if (!jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Family_Form).validate().form()) {
		
		// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
		jQuery(function() {
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Family_DATA__Problem ).dialog({
				  width: 560,
			      height: 240,
			      modal: true,
			      buttons: {
		                Ok: function () {
		                	jQuery(this).dialog("close");
		                }
		            }
			    });
			  });
		
		setTimeout(function(){ 
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Family_DATA__Problem ).dialog("close");
			},
			2470);
		// -------------------------------------------------------------------------------------------------------------------|/\|---
		
		
		return;
	}
	// ---------------------------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Group must have at least one member ----------------------------------------------------------------------------------------------------------|\/|---
	if ( this.get_FamilyMembers_Active_Number() < 1 ) {
		
		// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
		jQuery(function() {
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__No_Members_Found ).dialog({
				  width: 560,
			      height: 240,
			      modal: true,
			      buttons: {
		                Ok: function () {
		                	jQuery(this).dialog("close");
		                }
		            }
			    });
			  });
		
		setTimeout(function(){ 
			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__No_Members_Found ).dialog("close");
			},
			2470);
		// -------------------------------------------------------------------------------------------------------------------|/\|---
		
		
		return;
		
	}
	// ----------------------------------------------------------------------------------------------------------------------------------------------|/\|---	
	
	this.send_DATA();
	
};


/**
 * Edit family member
 * @param {FCJ_form_Add_Family_Event} event
 */
FCJ_form_Add_Famimly.prototype.manage_Event__edit_FamilyMember = function(event) {
	
	// Only works on list mode -------------------------------------------------------|\/|---
	if (this.config.form_Mode != FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST) {
		return;
	}
	// -------------------------------------------------------------------------------|/\|---
	
	// Initialize required objects --------------------------------------------------------------|\/|---
	var dni = null;	
	var family_Member_DATA = null;

	// ------------------------------------------------------------------------------------------|/\|---	
	
	
	// Set dni of family member -----------------|\/|---
	if (event.eventMessage.dni != null) {
		dni = event.eventMessage.dni;	
	}
	// ------------------------------------------|/\|---	
	
	
	family_Member_DATA = this.get_FamilyMember_By_DNI(dni);
	
	this.set_Form_MODE(FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_EDIT);
	
	this.setForm__FamilyMemberDATA(family_Member_DATA);

	// Exit this method if no family data is detected -----------------|\/|---
	if (family_Member_DATA == false) {
		return;
	}
	// ----------------------------------------------------------------|/\|---
	
	
};


/**
 * Delete family member
 * @param {FCJ_form_Add_Family_Event} (optional) event
 */
FCJ_form_Add_Famimly.prototype.manage_Event__delete_FamilyMember = function(event) {

	// Initialize required objects --------------------------------------------------------------|\/|---
	var dni = null;	
	var family_Member_DATA = null;
	// ------------------------------------------------------------------------------------------|/\|---	
	
	
	// Get member by DNI ------------------------------------------|\/|---
	if (event != null && event.eventMessage.dni != null) {
		dni = event.eventMessage.dni;	
	} 
	else if (this.config.others.selected_Member != null) {
		dni = this.config.others.selected_Member;
	}
	
	family_Member_DATA = this.get_FamilyMember_By_DNI(dni);
	// ------------------------------------------------------------|/\|---

	
	// Exit this method if no family member data is detected -----------------|\/|---
	if (family_Member_DATA == false) {
		return;
	}
	// -----------------------------------------------------------------------|/\|---

	
	// Hidde item in list (graphics) ----------------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Item + '__' +  family_Member_DATA._layer_ID)
		.css('display', 'none');
	// ----------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Mark mamber data as deleted -------------|-|---
	family_Member_DATA._deleted = true;
	
	
	// Update familiy members number counter --------------------------------------------------------------------------------------------------------|-|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_List_Options__Num_Members__Value).html(this.get_FamilyMembers_Active_Number()); 
	
};


/**
 * New family member
 * @param {FCJ_form_Add_Family_Event} event
 */
FCJ_form_Add_Famimly.prototype.manage_Event__new_FamilyMember = function(event) {

	// Clear member data form -------------------------------------------------------------------------|\/|---
	jQuery("#member_Form").clearForm();
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Member_isReferent).change();
	// ------------------------------------------------------------------------------------------------|/\|---
	
	
	// Set create mode -------------------------------------------------------|-|---
	this.set_Form_MODE(FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_CREATE);
	
};


/**
 * Show modal window style messages
 * @deprecated
 */
FCJ_form_Add_Famimly.prototype.modalWindow = function(message) {
	
	// Create a modal instance.
	var jQ_modal = jQuery('body').modal(),

    // Access an instance API
	jQ_modal_API = jQ_modal.data('modal');
	
	jQ_modal_API.open(message);
	
};


/**
 * Send data to server
 */
FCJ_form_Add_Famimly.prototype.send_DATA = function () {
	
	var form_AddFamily = this;
	
	var browser_DATA = [];
	
	
	// Read browser data -------------------------------------|\/|---
	jQuery.each(jQuery.browser, function(i, val) {
		
		var browser_Property = new Object();
		browser_Property.name = i;
		browser_Property.value = val;

		browser_DATA.push(browser_Property);
		
		});
	// -------------------------------------------------------|/\|---
	
	
	// Set family data --------------------------------------|-|---
	this.config.family_DATA = this.getForm__FamilyDATA();
	
	
	// Send data to server --------------------------------------------------------------------------|\/|---
	jQuery.ajax({
        type: 'POST',
        url: 'lib/form_ADD_Family.php?function=addFamily',
        data: { "family_DATA" : jQuery.toJSON(form_AddFamily.config.family_DATA),
        		"members_DATA" : jQuery.toJSON(form_AddFamily.config.familyMembers_DATA),
        		"browser_DATA" : jQuery.toJSON(browser_DATA)
        	},
        success: function(data) {
        	
        
        	var data_Object = jQuery.parseJSON(data); 	// Parse received data
        	
        	
        	switch (data_Object.type) {
        	
				case "ok":
					
					jQuery('#layer_ThankYou_Message').show();
		        	jQuery('#family_DATA').hide();
		        	jQuery('#layer__Member_DATA').hide();
		        	jQuery('#member_Form_Options').hide();
		        	jQuery('#members_List').hide();
		        	//members_List
					
					// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
		    		jQuery(function() {
		    			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Data_Sent_OK ).dialog({
		    				  width: 560,
		    			      height: 240,
		    			      modal: true,
		    			      buttons: {
		    		                Ok: function () {
		    		                	jQuery(this).dialog("close");
		    		                }
		    		            }
		    			    });
		    			  });
		    		
//		    		setTimeout(function(){ 
//		    			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_DATA__Problem ).dialog("close");
//		    			},
//		    			2470);
		    		// -------------------------------------------------------------------------------------------------------------------|/\|---

		        	
		        	// TODO FCJ Remove Debug ----------------------|\/|---	
		        	if (console != null) {
		        		console.log("Send data OK");
		            	console.log(data_Object);	
		        	}
		        	// --------------------------------------------|/\|---

					break;

					
				case "error":
		        	
		        	// Show modal window and set auto hide -------------------------------------------------------------------------------|\/|---
		        	jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Data_Submit_Error__Detail ).html( data_Object.error_Code + ' ' + data_Object.message );
		        	
		    		jQuery(function() {
		    			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Data_Submit_Error ).dialog({
		    				  width: 560,
		    			      height: 240,
		    			      modal: true,
		    			      buttons: {
		    		                Ok: function () {
		    		                	jQuery(this).dialog("close");
		    		                }
		    		            }
		    			    });
		    			  });
		    		
//		    		setTimeout(function(){ 
//		    			jQuery( '#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__ModalWindow__Member_DATA__Problem ).dialog("close");
//		    			},
//		    			2470);
		    		// -------------------------------------------------------------------------------------------------------------------|/\|---

					
					// TODO FCJ Remove Debug ----------------------|\/|---	
		        	if (console != null) {
		        		console.log("Send data ERROR");
		            	console.log(data_Object);	
		        	}
		        	// --------------------------------------------|/\|---
		        	
		        	
					break;
					
				default:
					break;
			}
        		
        		
        
        }
	});
	// ----------------------------------------------------------------------------------------------|/\|---
	
};



/**
 * Initialize configuration and some events
 */
FCJ_form_Add_Famimly.prototype.initialize = function() {

	// Initialize properties ----------------------------|\/|---
	var form_Add_Family = this;	// reference to this object
	this.config.family_DATA = new FCJ_Family_DATA();
	this.config.familyMembers_DATA = [];
	this.config.form_Mode = FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST;
	// --------------------------------------------------|/\|---
	
	
	// Initialize event driver ---------------------|\/|---
	this.event_Driver = new Event_Driver();
	this.event_Driver.delta_Trigger = 630;
	// ---------------------------------------------|/\|---
	
	
	// Check secure connection -----------------------------------------|\/|---
	if (location.protocol !== 'https:') {
		
		// TODO FCJ Remove Debug ----------------------|\/|---	
    	if (console != null) {
    		console.log("Page is not secure");
    	}
    	// --------------------------------------------|/\|---
    	
	}
	// -----------------------------------------------------------------|/\|---
	
	
	// Call to method for load refrence users ------------------|-|---
	this.load_RelatedUsers();
	
	
	// Initialize date of birth datepicker -------------------------------------------------|\/|---
	
	jQuery.datepicker.setDefaults( jQuery.datepicker.regional[ "ca" ] );
	
//	jQuery.datepicker.setDefaults( jQuery.datepicker.regional[ "*" ] );
//	jQuery( "#member_Date_of_birth" ).datepicker( jQuery.datepicker.regional[ "ca" ] );
	jQuery( "#member_Date_of_birth" ).datepicker({
		changeMonth: true,
		changeYear: true,
	    yearRange: "-190:+0", // Year range
	    showOn: "both",
//	    regional: jQuery.datepicker.regional[ "ca" ]
//	    buttonImage: "templates/images/calendar.gif", 
//	    buttonImageOnly: true 
	});
	
	
	
	// -------------------------------------------------------------------------------------|/\|---

	
	// Initialize validation rules -----------------------------------------------------------------------------------------------------------|\ Initialize validation rules /|---
	
	
//	jQuery.validator.addClassRules({
//		  name: {
//		    required: true,
//		    minlength: 2
//		  },
//		  zip: {
//		    required: true,
//		    digits: true,
//		    minlength: 5,
//		    maxlength: 5
//		  }
//		});
	
	
	// alias required to cRequired with new message
	jQuery.validator.addMethod("cRequired", jQuery.validator.methods.required,
	  "Aquesta dada es necessita.");
	// alias minlength, too
	jQuery.validator.addMethod("cMinlength", jQuery.validator.methods.minlength, 
	  // leverage parameter replacement for minlength, {0} gets replaced with 2
		jQuery.format("El mínim és de {0} caracters."));
	// combine them both, including the parameter for minlength
	jQuery.validator.addClassRules("customer", { cRequired: true, cMinlength: 2 });

	
	// validation method for DNI or NIE ----------------------------------------|\/|---
	jQuery.validator.addMethod("doc_DNI", function(value, element) { 
		
		var doc_OK = true;
		var check_doc_DNI = valida_nif_cif_nie(value);
		
		if (check_doc_DNI != 1 && check_doc_DNI != 3) {
			doc_OK = false;
		}
		
//		  return this.optional(element) || isValidNif(value);
		  return this.optional(element) || doc_OK; 
		  
		}, "El identificador no és correcte.");
	// -------------------------------------------------------------------------|/\|---
	
	
	// validate Group data form on keyup and submit -------------------------------------------|\ Validate group data /|---
	form_Add_Family.config.others.family_DATA__Validator = jQuery("#family_Form").validate({
		errorClass: "family_Form__Validation_Error",
		errorElement: "span",
		rules: {
			family: {
				cRequired: true,
				cMinlength: 2
			},
			phone: {
				cRequired: true,
				number: true,
				cMinlength: 9
			},
			mail: {
				cRequired: true,
				email: true
			},
			educator: {
				cRequired: true,
				cMinlength: 2
			}
//			password: {
//				required: true,
//				minlength: 5
//			},
//			confirm_password: {
//				required: true,
//				minlength: 5,
//				equalTo: "#password"
//			},
//			email: {
//				required: true,
//				email: true
//			},
//			topic: {
//				required: "#newsletter:checked",
//				minlength: 2
//			},
//			agree: "required"
		},
		messages: {
			family: {
				cRequired: "Introdueix el nom del grup.",
				cMinlength: "El nom del grup ha de tindre al menys {0} caracters."
			},
			phone: {
				cRequired: "Introdueix el telefon.",
				number: "Te que ser un número.",
				cMinlength: "El telèfon ha de tindre al menys {0} caracters."
			},
			educator: {
				cRequired: "Introdueix el nom del educador."
			},
			mail: {
				cRequired: "Introdueix el email.",
				email: "El format del email no és correcte."
			}
//			password: {
//				required: "Please provide a password",
//				minlength: "Your password must be at least 5 characters long"
//			},
//			confirm_password: {
//				required: "Please provide a password",
//				minlength: "Your password must be at least 5 characters long",
//				equalTo: "Please enter the same password as above"
//			},
//			email: "Please enter a valid email address",
//			agree: "Please accept our policy"
		}
	});
	// ----------------------------------------------------------------------------------------|/ Validate group data \|---
	
	
	
	// validate Member data form on keyup and submit -------------------------------------------|\ Validate member data /|---
	form_Add_Family.config.others.member_DATA__Validator = jQuery("#member_Form").validate({
		errorClass: "Member_Form__Validation_Error",
		errorElement: "span",
		rules: {
			member_Name: {
				cRequired: true,
				cMinlength: 2
			},
			member_SurName: {
				cRequired: true,
				cMinlength: 2
			},
			member_DNI: {
				cRequired: true,
				cMinlength: 2,
				doc_DNI: true
//				email: true
			},
			member_Date_of_birth: {
				cRequired: true,
				cMinlength: 2
			},
			member_Ref_Phone: {
				cRequired: true, //"#member_isReferent:unchecked",
				number: true,
				cMinlength: 9
			}
		},
		messages: {
			member_Name: {
				cRequired: "Introdueix el nom del membre.",
				cMinlength: "El nom del membre ha de tindre al menys {0} caracters"
			},
			member_SurName: {
				cRequired: "Introdueix el cognom del membre.",
				cMinlength: "El cognom del membre ha de tindre al menys {0} caracters"
			},
			member_DNI: {
				cRequired: "Introdueix el DNI del membre."
			},
			member_Date_of_birth: {
				cRequired: "Introdueix la data de neixement del membre."
//				email: "El formail del email no es correcte"
			},
			member_Ref_Phone: {
				cRequired: "Introdueix el telèfon",// 'input[name=member_isReferent][value=Yes]:checked';//"Introdueix el telefon.",
				number: "Te que ser un número.",
				cMinlength: "El telefon ha de tindre al menys {0} caracters"
			},
//			password: {
//				required: "Please provide a password",
//				minlength: "Your password must be at least 5 characters long"
//			},
//			confirm_password: {
//				required: "Please provide a password",
//				minlength: "Your password must be at least 5 characters long",
//				equalTo: "Please enter the same password as above"
//			},
//			email: "Please enter a valid email address",
//			agree: "Please accept our policy"
		}
	});
	// -----------------------------------------------------------------------------------------|/ Validate member data \|---
	
	// ---------------------------------------------------------------------------------------------------------------------------------------|/ Initialize validation rules \|---
	
	
	// Click add new member  -------------------------------------------------------------------------------|\/|---
	jQuery('#button_NewMember').bind('click', function() {
		
		var event = new FCJ_form_Add_Family_Event();
		event.eventType = FCJ_form_Add_Famimly__CONSTANTS.EVENT__Add_Member;
		event.eventMessage = "";
		
		form_Add_Family.event_Driver.notifyEvent(event, null, form_Add_Family);
		
	});
	// -----------------------------------------------------------------------------------------------------|/\|---	


	// Click new member ------------------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_New_Member).bind('click', function() {
		
		var event = new FCJ_form_Add_Family_Event();
		event.eventType = FCJ_form_Add_Famimly__CONSTANTS.EVENT__New_Member;
		event.eventMessage = "";
		
		form_Add_Family.event_Driver.notifyEvent(event, null, form_Add_Family);
		
	});
	// -----------------------------------------------------------------------------------------------------|/\|---

	
	// Click cancel new member -----------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Cancel_New_Member).bind('click', function() {
		
		var event = new FCJ_form_Add_Family_Event();
		event.eventType = FCJ_form_Add_Famimly__CONSTANTS.EVENT__Cancel_New_Member;
		event.eventMessage = "";
		
		form_Add_Family.event_Driver.notifyEvent(event, null, form_Add_Family);
		
	});
	// -----------------------------------------------------------------------------------------------------|/\|---
	
	
	// Click update member ---------------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_UpdateMember).bind('click', function() {
		
		var event = new FCJ_form_Add_Family_Event();
		event.eventType = FCJ_form_Add_Famimly__CONSTANTS.EVENT__Update_Member;
		event.eventMessage = "";
		
		form_Add_Family.event_Driver.notifyEvent(event, null, form_Add_Family);
		
	});
	// -----------------------------------------------------------------------------------------------------|/\|---
	
	
	// Click send data  ------------------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Send_Data).bind('click', function() {
		
		var event = new FCJ_form_Add_Family_Event();
		event.eventType = FCJ_form_Add_Famimly__CONSTANTS.EVENT__Send_Data;
		event.eventMessage = "";
		
		form_Add_Family.event_Driver.notifyEvent(event, null, form_Add_Family);
		
	});
	// -----------------------------------------------------------------------------------------------------|/\|---
	
	
	// Click reload form -----------------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_ReloadForm).bind('click', function() {
		
		form_Add_Family.reload_Form();
		
	});
	// -----------------------------------------------------------------------------------------------------|/\|---
	
	
	// Click {Button for referent family members} -------------------------------------------------------------------------|\/|---
	jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Member_isReferent).bind(
			'change', 
			function(event, param1) {
				
				if (jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Member_isReferent).is(':checked')) {
					jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Ref_Phone).show();
				} else {
					jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Member_Ref_Phone).hide();
				}

				});
	// --------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Click {Button for other transport type) ----------------------------------------------------------------------------|\/|---
	jQuery('input[name=' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Tranport_Type + ']').bind(
			'change', 
			function(event, param1) {
				
				var selected = jQuery("input[name='" + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Button_Tranport_Type + "']:checked").val();

				switch (selected) {
					case 'other':
						jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Transport_Type__OTHER_Detail).show();
						break;
	
					default:
						jQuery('#' + FCJ_form_Add_Famimly__CONSTANTS.LAYER_ID__Transport_Type__OTHER_Detail).hide();
						break;
					}
				
				});
	// --------------------------------------------------------------------------------------------------------------------|/\|---
	
	
	// Modal window for delete member ------------------------------------------------------|\/|---
	jQuery('#modalWindow__Member_Delete').dialog({
			autoOpen: false,
			modal: true,
			buttons: [ 
			           { text: "Ok", click: function() { 
			        	
			        	   form_Add_Family.manage_Event__delete_FamilyMember();
			        	   
				           	// TODO FCJ Remove Debug ----------------------|\/|---	
				           	if (console != null) {
				           		console.log("Delete member OK");
				           	}
				           	// --------------------------------------------|/\|---
			           	
			           	
			        	   jQuery( this ).dialog( "close" ); } 
			           },
			           
			           { text: "Cancel", click: function() { 
			        	   
				           	// TODO FCJ Remove Debug ----------------------|\/|---	
				           	if (console != null) {
				           		console.log("Delete member Cancel");
				           	}
				           	// --------------------------------------------|/\|---
				           	
			        	   jQuery( this ).dialog( "close" ); } 
			           }
			         ] 
			});
	// -------------------------------------------------------------------------------------|/\|---	
	
	
	// Modal window for cancel new member --------------------------------------------------|\/|---
	jQuery('#modalWindow__Member_CancelNew').dialog({
			autoOpen: false,
			modal: true,
			buttons: [ 
			           { text: "Ok", click: function() { 
			        	
			        	   form_Add_Family.set_Form_MODE(FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST);
			        	   
				           	// TODO FCJ Remove Debug ----------------------|\/|---	
				           	if (console != null) {
				           		console.log("Cancel new member OK");
				           	}
				           	// --------------------------------------------|/\|---
			           	
			           	
			        	   jQuery( this ).dialog( "close" ); } 
			           },
			           
			           { text: "Cancel", click: function() { 
			        	   
				           	// TODO FCJ Remove Debug ----------------------|\/|---	
				           	if (console != null) {
				           		console.log("New member not canceled");
				           	}
				           	// --------------------------------------------|/\|---
				           	
				        	   jQuery( this ).dialog( "close" ); } 
			           }
			         ] 
			});
	// -------------------------------------------------------------------------------------|/\|---	

	
	// Map event {Add member} -------------------------------------------------|\/|---
	jQuery(form_Add_Family).bind(
			FCJ_form_Add_Famimly__CONSTANTS.EVENT__Add_Member, 
			function(event, param1) {
				form_Add_Family.manage_Event__add_FamilyMember();
				});
	// ------------------------------------------------------------------------|/\|---
	
	
	// Map event {Edit member} -------------------------------------------------|\/|---
	jQuery(form_Add_Family).bind(
			FCJ_form_Add_Famimly__CONSTANTS.EVENT__Edit_Member, 
			function(event, param1) {
				form_Add_Family.manage_Event__edit_FamilyMember(param1);
				});
	// ------------------------------------------------------------------------|/\|---
	
	
	// Map event {Delete member} --------------------------------------------------------------------------------|\/|---
	jQuery(form_Add_Family).bind(
			FCJ_form_Add_Famimly__CONSTANTS.EVENT__Delete_Member, 
			function(event, param1) {
				
				// Only works on list mode ------------------------------------------------------------------|\/|---
				if (form_Add_Family.config.form_Mode != FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST) {
					return;
				}
				// ------------------------------------------------------------------------------------------|/\|---
				
				// Get member DNI ------------------------------------------------------------|\/|---
				if (param1.eventMessage.dni != null) {
					form_Add_Family.config.others.selected_Member = param1.eventMessage.dni;	
				}
				// ---------------------------------------------------------------------------|/\|---
				
				jQuery('#modalWindow__Member_Delete').dialog( "open" );
//				form_Add_Family.manage_Event__delete_FamilyMember(param1);
				});
	// ----------------------------------------------------------------------------------------------------------|/\|---
	

	// Map event {New member) -------------------------------------------------|\/|---
	jQuery(form_Add_Family).bind(
			FCJ_form_Add_Famimly__CONSTANTS.EVENT__New_Member, 
			function(event, param1) {
				form_Add_Family.manage_Event__new_FamilyMember(param1);
				});
	// ------------------------------------------------------------------------|/\|---
	

	// Map event {Cancel New member) ------------------------------------------|\/|---
	jQuery(form_Add_Family).bind(
			FCJ_form_Add_Famimly__CONSTANTS.EVENT__Cancel_New_Member, 
			function(event, param1) {
				jQuery('#modalWindow__Member_CancelNew').dialog( "open" );
				});
	// ------------------------------------------------------------------------|/\|---
	
	
	// Map event {Update member) ----------------------------------------------|\/|---
	jQuery(form_Add_Family).bind(
			FCJ_form_Add_Famimly__CONSTANTS.EVENT__Update_Member, 
			function(event, param1) {
				form_Add_Family.manage_Event__update_FamilyMember();
//				form_Add_Family.
//				jQuery('#modalWindow__Member_CancelNew').dialog( "open" );
				});
	// ------------------------------------------------------------------------|/\|---
	
	
	// Map event {Send data} --------------------------------------------------|\/|---
	jQuery(form_Add_Family).bind(
			FCJ_form_Add_Famimly__CONSTANTS.EVENT__Send_Data, 
			function(event, param1) {
				
				// Only works on list mode ------------------------------------------------------------------|\/|---
				if (form_Add_Family.config.form_Mode != FCJ_form_Add_Famimly__CONSTANTS.FORM_MODE_LIST) {
					return;
				}
				// ------------------------------------------------------------------------------------------|/\|---
				
				
				form_Add_Family.manage_Event__send_DATA(param1);
				});
	// ------------------------------------------------------------------------|/\|---

};


/**
 * Form add Family configuration
 * 
 * Is like object properties
 * 
 */
FCJ_form_Add_Famimly.prototype.config = {
	"family_DATA" : "",
	"familyMembers_DATA" : [],
	"form_Mode" : "",
	"referent_Users" : {
		"ref_Users_DATA" : []
	},
	"others" : {
		selected_Member : null,
		family_DATA__Validator: null,
		member_DATA__Validator: null
	}
};


/**
 * Form add family
 */
function FCJ_form_Add_Famimly() {
	
	this.initialize();
	
}


// Create new form to add family
var form_AddFamily = null;
jQuery(document).ready(function($){

	form_AddFamily = new FCJ_form_Add_Famimly();
	});

